package com.emm.yixun.website.service;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
@Component
public class CacheUtils {
	private static final Logger logger = LoggerFactory.getLogger(CacheUtils.class);
	
	@Autowired
	private CacheManager cacheManager; 
	
	public void put(String cacheName,String key,Object object){
		try {
			Cache  cache = cacheManager.getCache(cacheName);
			Element element = new Element(key,object);
			cache.put(element);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("缓存("+key+")失败:"+e);
		}
	}
	
	public Object get(String cacheName,String key){
		try {
			Cache  cache = cacheManager.getCache(cacheName);
			Element element=cache.get(key);
			return element.getObjectValue();
		} catch (Exception e) {
			logger.error("获取缓存("+key+")失败:"+e);
		}
		return null;
		
	}
	
	 public void remove(String cacheName, String key) {  
	       try {
			Cache cache = cacheManager.getCache(cacheName);  
			   cache.remove(key);
		} catch (Exception e) {
			logger.error("清除缓存("+key+")失败:"+e);
		}   
	 } 
}
